package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.entity.Course;
import edu.suda.xsap.service.CourseService;
import edu.suda.xsap.dto.CourseDto;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        courseService.delete(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list() {
        List<CourseDto> courseDtoList = courseService.findAll();
        return Result.success(courseDtoList);
    }
//    @GetMapping("/get")
//    public Result get(@RequestParam("id") Integer id) {
//        CourseDto courseDto = courseService.findById(id);
//        return Result.success(courseDto);
//    }
    @PostMapping("/add")
    public Result save(@RequestBody Course course) {
        courseService.save(course);
        return Result.success();
    }
    @PutMapping("/update")
    public Result update(@RequestBody Course course) {
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/bind")
    public Result bind(@RequestParam("id") Integer id) {
        CourseDto courseDto = courseService.findById(id);
        return Result.success(courseDto);
    }
}
